<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Usr2grp extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "usr2grp";
		$this->prefix = "u2g";
	}

	function getUsr2grp($where = NULL) {

		$qs =	"
					select
						u2g_id, u2g_grp_id
					from
						".$this->db->dbprefix.$this->tableName."
					where
						u2g_del = 0".((!is_null($where))?(" and ".$where):(""));
		$result = $this->db->query($qs);
		$getUsr2grp = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$getUsr2grp[$row->u2g_grp_id] = $row->u2g_id;
			}
		}

		return $getUsr2grp;
	}

	function checkInsRegistration($usrId) { // TODO
		$qs =	"
					select
						coalesce(u2g_ins_id, u2g_ins_name) as ins
					from
						".$this->db->dbprefix.$this->tableName."
					where
						u2g_del = 0 and u2g_usr_id = '".$this->db->escape_str($usrId)."' ";
		$result = $this->db->query($qs);
		$getUsr2grp = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				return (($row->ins)?(TRUE):(FALSE));
			}
		}

		return FALSE;
	}
	
	function getUsr2grpId($where = NULL) {
		$qs =	"
					select
						u2g_id
					from
						".$this->db->dbprefix.$this->tableName."
					where
						u2g_del = 0".((!is_null($where))?(" and ".$where):(""));
		$result = $this->db->query($qs);
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				return $row->u2g_id;
			}
		}

		return FALSE;
	}
	
}